use anyhow::Ok;

use opencv::prelude::*;
use video_img_find::{img, video_cap::*};

const VIDEO_PATH: &str = "/home/jaakan/下载/超级飞侠9/15.mp4";

#[test]
fn read_and_save_img() -> anyhow::Result<()> {
    let mut video_cap = open_video_cap(VIDEO_PATH)?;
    set_cap_pos_ms(&mut video_cap, 300_000_f64)?;
    next_frame(&mut video_cap)?;

    // 读取帧
    let mut frame = Mat::default();
    read_frame(&mut video_cap, &mut frame)?;

    img::save_rgb_img_width_frame("./11111.jpg", &frame)?;

    Ok(())
}

#[test]
fn test_find_img_pos_ms() {
    let video_path = "/home/jaakan/下载/超级飞侠9/14.mp4";

    // 跳过时间
    let skip_ms = 60_000f64;
    let pos_ms = find_img_pos_ms(video_path, "./target.jpg", skip_ms, None).unwrap();

    if pos_ms.is_none() {
        ()
    }

    let cmd = format!(
        "ffmpeg -ss 0.0 -to '{}ms' -i '{}' -y -f mp4 -vcodec copy -acodec copy -q:v 1 out_1.mp4",
        pos_ms.unwrap(),
        video_path,
    );

    println!("{cmd}");

    let pos_ms = pos_ms.unwrap() + 18_f64;
    let cmd = format!(
        "ffmpeg -ss '{}ms' -i '{}' -y -f mp4 -vcodec copy -acodec copy -q:v 1  out_2.mp4",
        pos_ms,
        video_path,
    );
    
    println!("{cmd}");
}
